import 'package:base_flutter/base_flutter.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:ls_work/config/global_configs.dart';
import 'package:ls_work/message/message_config.dart';
import 'package:ls_work/model/building_model.dart';
import 'package:ls_work/net/post_request.dart';
import 'package:ls_work/net/response/service_execute/service_execute_statistics_data.dart';

import '../../a_yyt/models/floor_list_entity.dart';

class ServiceStatisticsViewModel
    extends BaseListViewModel<ServiceExecuteStatisticsDataList> {
  final String status;

  ServiceStatisticsViewModel(BuildContext context, this.status)
      : super(context);
  var today = DateTime.now().toDateStr([yyyy, '-', mm, '-', dd]);
  ServiceExecuteStatisticsData? statisticsData;

  String fuzzyQuery = "";

  var buildingModel = BuildingModel();
  var buildingList = <FloorEntity>[];
  var floorList = <FloorInfo>[];
  FloorEntity? selectedBuilding;
  FloorInfo? selectedFloor;

  @override
  Future<List<ServiceExecuteStatisticsDataList>> requestListData() async {
    String? customerIdStr;
    if(GlobalConfigs.nurseUseFilter){
      if((selectedBuilding?.iId??"").isNullOrEmpty()&&(selectedFloor?.iId ?? "").isNullOrEmpty()){
        customerIdStr = GlobalConfigs.savedFilterElder.map((e)=>e.id).join(",");
      }
    }
    var data = await PostRequest.serviceExecuteStatistics(
        status: status,
        planFinishDateFin1: today,
        planFinishDateFin2: today,
        fuzzyQuery: fuzzyQuery,
        page: page,
        rows: pageLength,
        buildingId: selectedBuilding?.iId ?? "",
        floorId: selectedFloor?.iId ?? "",customerIdStr:customerIdStr);
    if ('0' == data['code'].toString()) {
      statisticsData = ServiceExecuteStatisticsData.fromJson(data['data']);
      sendMessage(SendMessageEvent(MessageConfig.SERVICE_HOME_STATISTICS,
          obj: StatisticsData(
              total: statisticsData?.totalMession.toString() ?? "",
              hasServed: statisticsData?.finishedCount.toString() ?? "",
              notServed: statisticsData?.waitCount.toString() ?? "")));
      return statisticsData?.rows ?? [];
    } else {
      throw data['msg'];
    }
  }

  @override
  void onCreated() {
    super.onCreated();
    getBuildingList();
    requestRefresh();
  }

  Future<void> getBuildingList() async {
    try {
      buildingList = await buildingModel.getBuildingList();
      buildingList.insert(
          0,
          FloorEntity(
              buildingNo: '全部楼栋', floorList: [FloorInfo(floorNo: "全部楼层")]));
    } catch (e) {
      buildingList = [
        FloorEntity(buildingNo: '全部楼栋', floorList: [FloorInfo(floorNo: "全部楼层")])
      ];
    }
    if (buildingList.isNotEmpty) {
      selectedBuilding = buildingList[0];
    }
    if ((selectedBuilding?.floorList ?? []).isNotEmpty) {
      selectedFloor = selectedBuilding?.floorList![0];
    }

    notifyListeners();
  }

  Future<void> selectBuilding() async {
    if (buildingList.isEmpty) {
      await getBuildingList();
    }
    DataPickerView(context, buildingList, onSubmitListener: (p1, p2, p3) {
      selectedBuilding = buildingList[p1];
      if (selectedBuilding!.floorList![0].iId.isNotEmpty) {
        selectedBuilding!.floorList!.insert(0, FloorInfo(floorNo: "全部楼层"));
      }
      selectedFloor = selectedBuilding!.floorList![0];
      notifyListeners();
      requestRefresh();
    }).show();
  }

  void selectFloor() {
    DataPickerView(context, selectedBuilding?.floorList ?? [],
        onSubmitListener: (p1, p2, p3) {
      selectedFloor = selectedBuilding!.floorList![p1];
      notifyListeners();
      requestRefresh();
    }).show();
  }
}

class StatisticsData {
  final String total;
  final String hasServed;
  final String notServed;

  StatisticsData(
      {this.total = "0", this.hasServed = "0", this.notServed = "0"});
}
